package cn.com.fig.control;

import cn.com.fig.common.*;
import cn.com.fig.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/baseControl")
public class BaseControl {
	@Autowired
	protected BaseService baseService;
	
	/**
	 * 检索操作
	 * @param statement
	 * @param param
	 * @param reqParam
	 * @return
	 */
	@RequestMapping("/GET/{statement}")
	public PaginationResponse<Map<String,Object>> queryList(@PathVariable String statement,
			@RequestParam Map<String,Object> param,PaginationReqParam reqParam){
		
		return baseService.queryList(statement, param, reqParam);
	}
	/**
	 * 新增操作
	 * @param statement
	 * @param department
	 * @return
	 */
	@RequestMapping("/PUT/{statement}")
	public ReplyMsg insert(@PathVariable String statement,
						   @RequestParam Map<String,Object> department){

		return baseService.save(statement, department);
	}
	
	/**
	 * 批量新增操作
	 * @param statement
	 * @param param
	 * @param keyGen 主键生成规则 UUID(按UUID规则生成)、DB（由数据库生成）、AL（页面产生）
	 * @param primary 表示主键得key
	 * @return
	 */
	@RequestMapping("/BATCHPUT/{statement}")
	public ReplyMsg insert(@PathVariable String statement,
			@RequestParam(value="param",required=false) String param,String keyGen,String primary){
		List<Map<String, Object>> list = JsonUtils.decodeList(param);
		for(int i=0; i<list.size(); i++){
			if("UUID".equals(keyGen)){
				list.get(i).put(primary, UuidGen.genUUID());
			}
		}
		return baseService.insertBatch(statement, list);
	}
	/**
	 * 更新操作
	 * @param statement
	 * @param department
	 * @return
	 */
	@RequestMapping("/POST/{statement}")
	public ReplyMsg save(@PathVariable String statement,
			@RequestParam Map<String,Object> department){
		
		return baseService.save(statement, department);
	}
	/**
	 * 删除操作
	 * @param statement
	 * @param priId
	 * @return
	 */
	@RequestMapping("/DELETE/{statement}")
	public ReplyMsg del(@PathVariable String statement,
			String priId){
		
		return baseService.del(statement, priId);
	}
}
